# -*- coding: utf-8 -*-

from django.conf import settings
from django.shortcuts import get_object_or_404, render_to_response
from django.template.context import RequestContext
from django.http import HttpResponsePermanentRedirect

from models import StructureNode


def structurenode(request, path):
    if settings.APPEND_SLASH and path and not path.endswith('/'):
        return HttpResponsePermanentRedirect('/%s/' % path)
    path = '/%s' % (path)
    obj = get_object_or_404(StructureNode, path=path)
    if obj.redirect_url:
        return HttpResponsePermanentRedirect(obj.redirect_url)
    return render_to_response(obj.get_template(), 
                              dict(object=obj, root=obj.get_root()), 
                              RequestContext(request))
    
